package ph.com.gs3.formalistics.view.document.specialfields;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.model.vo.business.form.FormFieldData;
import ph.com.gs3.formalistics.view.document.abstractfields.TextTypeFieldView;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class BarcodeScannerField extends TextTypeFieldView {

	public static final String TAG = BarcodeScannerField.class.getSimpleName();

	private TextView tvLabel;
	private Button bScanCode;

	private BarcodeScannerListener listener;

	public BarcodeScannerField(Context context, FormFieldData formField) {
		super(context, formField);

		createView();

		setLabel(formField.getLabel());
		setValue(formField.getValue());
	}

	protected void createView() {

		LayoutInflater.from(getContext()).inflate(R.layout.field_barcode_scanner, this, true);

		tvLabel = (TextView) findViewById(R.id.Barcode_tvLabel);
		setEditTextValueField((EditText) findViewById(R.id.Barcode_etScannedCode));
		bScanCode = (Button) findViewById(R.id.Barcode_bScanCode);
		bScanCode.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				if (listener != null) {
					listener.onScanCodeCommand(BarcodeScannerField.this);
				}
			}
		});

		this.setTag(fieldId + "_container");
		getEditTextValueField().setTag(fieldId);

	}

	// =================================================================================
	// {{ Getters & Setters

	@Override
	public void setEnabled(boolean enabled) {

		getEditTextValueField().setEnabled(enabled);
		bScanCode.setEnabled(enabled);
		bScanCode.setClickable(enabled);

	}

	public void setLabel(String label) {
		tvLabel.setText(label);
	}

	public String getLabel() {
		return tvLabel.getText().toString();
	}

	@Override
	public String getValue() {
		return getEditTextValueField().getText().toString();
	}

	@Override
	public void setValue(String value) {
		getEditTextValueField().setText(value);
	}

	// }}

	// =====================================================================
	// {{ Interface(s)

	public BarcodeScannerListener getListener() {
		return listener;
	}

	public void setListener(BarcodeScannerListener listener) {
		this.listener = listener;
	}

	public static interface BarcodeScannerListener {

		public void onScanCodeCommand(BarcodeScannerField source);

	}

	// }}

}
